/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.restaurant.entity.CommodityRecordRelation;
import org.springblade.modules.restaurant.service.ICommodityRecordRelationService;
import org.springblade.modules.restaurant.vo.CommodityRecordRelationVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * 货物采购明细表 控制器
 *
 * @author Blade
 * @since 2025-01-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/restaurant/commodityrecordrelation")
@Tag(name = "货物采购明细表", description = "货物采购明细表接口")
public class CommodityRecordRelationController extends BladeController {

	private ICommodityRecordRelationService commodityRecordRelationService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入commodityRecordRelation")
	public R<CommodityRecordRelation> detail(CommodityRecordRelation commodityRecordRelation) {
		CommodityRecordRelation detail = commodityRecordRelationService.getOne(Condition.getQueryWrapper(commodityRecordRelation));
		return R.data(detail);
	}

	/**
	 * 分页 货物采购明细表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入commodityRecordRelation")
	public R<IPage<CommodityRecordRelation>> list(@ParameterObject CommodityRecordRelation commodityRecordRelation, @ParameterObject Query query) {
		IPage<CommodityRecordRelation> pages = commodityRecordRelationService.page(Condition.getPage(query), Condition.getQueryWrapper(commodityRecordRelation));
		return R.data(pages);
	}

	/**
	 * 自定义分页 货物采购明细表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Operation(summary = "分页", description = "传入commodityRecordRelation")
	public R<IPage<CommodityRecordRelationVO>> page(@ParameterObject CommodityRecordRelationVO commodityRecordRelation, @ParameterObject Query query) {

		String purchaseTime = commodityRecordRelation.getPurchaseTime();
		if (StrUtil.isNotBlank(purchaseTime)) {
			String startTime = DateUtil.formatDateTime(DateUtil.beginOfDay(DateUtil.parse(purchaseTime, DatePattern.NORM_DATE_PATTERN)));
			String endTime = DateUtil.formatDateTime(DateUtil.endOfDay(DateUtil.parse(purchaseTime, DatePattern.NORM_DATE_PATTERN)));
			commodityRecordRelation.setStartTime(startTime);
			commodityRecordRelation.setEndTime(endTime);
		}

		IPage<CommodityRecordRelationVO> pages = commodityRecordRelationService.selectCommodityRecordRelationPage(Condition.getPage(query), commodityRecordRelation);
		return R.data(pages);
	}

	/**
	 * 新增 货物采购明细表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Operation(summary = "新增", description = "传入commodityRecordRelation")
	public R save(@Valid @RequestBody CommodityRecordRelation commodityRecordRelation) {
		return R.status(commodityRecordRelationService.save(commodityRecordRelation));
	}

	/**
	 * 修改 货物采购明细表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Operation(summary = "修改", description = "传入commodityRecordRelation")
	public R update(@Valid @RequestBody CommodityRecordRelation commodityRecordRelation) {
		return R.status(commodityRecordRelationService.updateById(commodityRecordRelation));
	}

	/**
	 * 新增或修改 货物采购明细表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入commodityRecordRelation")
	public R submit(@Valid @RequestBody CommodityRecordRelation commodityRecordRelation) {
		return R.status(commodityRecordRelationService.saveOrUpdate(commodityRecordRelation));
	}


	/**
	 * 删除 货物采购明细表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@Operation(summary = "逻辑删除", description = "传入ids")
	public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
		return R.status(commodityRecordRelationService.deleteLogic(Func.toLongList(ids)));
	}


}
